package com.neurotech.pcc.widgets

import android.content.Context
import android.support.v4.content.ContextCompat
import android.text.InputFilter
import android.text.InputType
import android.util.AttributeSet
import android.view.Gravity
import android.view.ViewGroup
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.TextView
import com.neurotech.pcc.R


/**
 * 端口输入框
 * Created by shenhua on 2018/10/11.
 * @author shenhua
 *         Email shenhuanet@126.com
 */
class PortInputLayout @JvmOverloads constructor(context: Context, attributeSet: AttributeSet? = null, defAttrStyle: Int = 0)
    : LinearLayout(context, attributeSet, defAttrStyle) {

    init {
        orientation = HORIZONTAL
        val layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        addView(DotTextView(context))
        addView(PortEditText(context), layoutParams)
    }

    fun fitPort(port: String?) {
        (getChildAt(1) as EditText).setText(port)
    }

    fun port(): String {
        return (getChildAt(1) as EditText).text.toString()
    }

    inner class PortEditText constructor(context: Context) : EditText(context) {

        init {
            hint = "9"
            maxLines = 1
            filters = arrayOf<InputFilter>(InputFilter.LengthFilter(8))
            inputType = InputType.TYPE_CLASS_NUMBER
            gravity = Gravity.CENTER
            setTextColor(ContextCompat.getColor(context, R.color.colorPrimary))
        }
    }

    inner class DotTextView constructor(context: Context) : TextView(context) {
        init {
            text = " : "
            gravity = Gravity.BOTTOM
        }
    }
}